#include "stdafx.h"
#include "ui_commandedit.h"
//Added by qt3to4:
#include <QFocusEvent>
#include <QKeyEvent>
#include <QEvent>


#include "pf_debug.h"


/**
 * Default Constructor. You must call init manually if you choose
 * to use this constructor.
 */
UI_CommandEdit::UI_CommandEdit(QWidget* parent, const char* name)
        : QLineEdit(parent, name) {}



/**
 * Destructor
 */
UI_CommandEdit::~UI_CommandEdit() {
}


/**
 * Bypass for key press events from the tab key.
 */
bool UI_CommandEdit::event(QEvent* e) {
	if (e->type()==QEvent::KeyPress) {
		QKeyEvent* k = (QKeyEvent*)e;
		if (k->key()==Qt::Key_Tab) {
			emit tabPressed();
			return true;
		}
	}
	
	return QLineEdit::event(e);
}



/**
 * History (arrow key up/down) support, tab.
 */
void UI_CommandEdit::keyPressEvent(QKeyEvent* e) {

	switch (e->key()) {
            case Qt::Key_Up:
                    if (!historyList.isEmpty() && it>historyList.begin()) {
                            it--;
                            setText(*it);
                    }
                    break;

            case Qt::Key_Down:
                    if (!historyList.isEmpty() && it<historyList.end() ) {
                            it++;
                            if (it<historyList.end()) {
                                    setText(*it);
                            }
                            else {
                                    setText("");
                            }
                    }
                    break;

            case Qt::Key_Return:
                    historyList.append(text());
                    it = historyList.end();
                    QLineEdit::keyPressEvent(e);
                    break;

            case Qt::Key_Escape:
                    if (text().isEmpty()) {
                            emit escape();
                    }
                    else {
                            setText("");
                    }
                    break;

            default:
                    QLineEdit::keyPressEvent(e);
                    break;
	}
}


void UI_CommandEdit::focusInEvent(QFocusEvent *e) {
	emit focusIn();
	QLineEdit::focusInEvent(e);
}


void UI_CommandEdit::focusOutEvent(QFocusEvent *e) {
	emit focusOut();
	QLineEdit::focusOutEvent(e);
}


